<?php
$this->jQuery()->enable()->uiEnable();
$this->headLink()
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid.css'))
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid-custom.css'));
$this->headScript()
        ->appendFile($this->baseUrl('/lib/flexigrid-1.1/js/flexigrid.js'));
$this->pagetitle = 'Quản lý tài sản';
$uRole = Zend_Auth::getInstance()->getIdentity()->Role;
?>

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Tài sản bạn mượn</a></li>
        <li><a href="#tabs-2">Tài sản đang cho mượn</a></li>
        <li><a href="#tabs-3">Tài sản có thể mượn</a></li>
        <li><a href="#tabs-4">Tài sản đang hỏng</a></li>
        <li><a href="#tabs-5">Tất cả tài sản</a></li>
    </ul>
    <div id="tabs-1">
        <table class='gridTable1' style="display:none"></table>
    </div>
    <div id="tabs-2">
        <table class='gridTable2' style="display:none"></table>
    </div>
    <div id="tabs-3">
        <table class='gridTable3' style="display:none"></table>
    </div>
    <div id="tabs-4">
        <table class='gridTable4' style="display:none"></table>
    </div>
    <div id="tabs-5">
        <table class='gridTable5' style="display:none"></table>
    </div>
</div>
<?php if ($uRole == 0 || $uRole == 1) : ?>
    <div id="dialog-form" style="display: none">
        <p class="validateTips">All form fields are required.</p>
        <?php echo $this->form; ?>
    </div>
    <div id="dialog-confirm" title="Delete user confirm?" style="display: none">
        <p>
            <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
            Information of item will be permanently deleted and cannot be recovered. Are you sure?
        </p>
    </div>
<?php endif; ?>

<script type="text/javascript">
    <!--
    $(document).ready(function(){
        $( "#tabs" ).tabs();
        
        var gridTable1 = $(".gridTable1");
        var gridTable2 = $(".gridTable2");
        var gridTable3 = $(".gridTable3");
        var gridTable4 = $(".gridTable4");
<?php if ($uRole == 0 || $uRole == 1) : ?>
            var gridTable5 = $(".gridTable5");
<?php endif; ?>
        $("#tabs-1").click(fDataGrid(gridTable1,'<?php echo $this->url(array('module' => 'asset', 'controller' => 'loan', 'action' => 'records', 'mode' => 2)); ?>'));
        $("#tabs-2").click(fDataGrid(gridTable2,'<?php echo $this->url(array('module' => 'asset', 'controller' => 'item', 'action' => 'records', 'mode' => 3)); ?>'));
        $("#tabs-3").click(fDataGrid(gridTable3,'<?php echo $this->url(array('module' => 'asset', 'controller' => 'item', 'action' => 'records', 'mode' => 2)); ?>'));
        $("#tabs-4").click(fDataGrid(gridTable4,'<?php echo $this->url(array('module' => 'asset', 'controller' => 'item', 'action' => 'records', 'mode' => 4)); ?>'));
<?php if ($uRole == 0 || $uRole == 1) : ?>
            $("#tabs-5").click(fDataGrid(gridTable5,'<?php echo $this->url(array('module' => 'asset', 'controller' => 'item', 'action' => 'records', 'mode' => 1)); ?>'));
            var tips = $(".validateTips");
            var itemid = $("#ItemID"),
            maTS = $("#MaTS"),
            tenTS = $("#TenTS"),
            descr = $("#Description"),
            type = $("#Type"),
            startDate = $("#StartDate"),
            price = $("#Price"),
            warrantyTime = $("#WarrantyTime"),
            status = $("#Status"),
            place = $("#Place"),
            allFields = $( [] ).add(itemid).add(maTS).add(tenTS).add(descr).add(type).add(startDate).add(price).add(warrantyTime).add(status).add(place);
                
            $( "#StartDate" ).datepicker({ dateFormat: 'yy-mm-dd' });
                
            function updateTips( t ) {
                tips
                .text( t )
                .addClass( "ui-state-highlight" );
                setTimeout(function() {
                    tips.removeClass( "ui-state-highlight", 1500 );
                }, 500 );
            }

            function checkLength( o, n, min, max ) {
                if ( o.val().length > max || o.val().length < min ) {
                    o.addClass( "ui-state-error" );
                    updateTips( "Length of " + n + " must be between " +
                        min + " and " + max + "." );
                    return false;
                } else {
                    return true;
                }
            }

            function checkRegexp( o, regexp, n ) {
                if ( !( regexp.test( o.val() ) ) ) {
                    o.addClass( "ui-state-error" );
                    updateTips( n );
                    return false;
                } else {
                    return true;
                }
            }
<?php endif; ?>
        function fDataGrid(gTB, t){
            gTB.flexigrid({
                url : t,
                dataType : 'json',
                colModel : [ 
                    //                {display: 'Item ID', name: 'ItemID', width: 50, sortable: true, align: 'left'},
                    {display: 'Mã TS', name: 'Ma_tai_san', width: 60, sortable: true, align: 'left'},
                    {display: 'Tên tài sản', name: 'Ten_tai_san', width: 100, sortable: true, align: 'left'},
                    {display: 'Mô tả', name: 'Description', width: 300, sortable: false, align: 'left'},
                    {display: 'Loại bảo mật', name: 'Type', width: 70, sortable: true, align: 'left'},
                    {display: 'Bắt đầu SD', name: 'StartDate', width: 70, sortable: true, align: 'left'},
                    {display: 'Giá', name: 'Price', width: 60, sortable: true, align: 'left'},
                    {display: 'Bảo hành', name: 'WarrantyTime', width: 80, sortable: true, align: 'left'},
                    {display: 'Tình trạng', name: 'Status', width: 80, sortable: true, align: 'left'},
                    {display: 'Địa điểm hiện tại', name: 'Place', width: 100, sortable: true, align: 'left'}
                ],
                buttons : [ 
<?php if ($uRole == 0) : ?>{name : 'Add', bclass : 'add', onpress : add_record},<?php endif; ?>
<?php if ($uRole == 0 || $uRole == 1) : ?>{name : 'Edit', bclass : 'edit', onpress : edit_record},<?php endif; ?>
<?php if ($uRole == 0) : ?>{name : 'Delete', bclass : 'delete', onpress : delete_record}, <?php endif; ?>
                    {separator : true},
                    {name : 'Detail', bclass : 'detail', onpress : detail_record}
                ],
                searchitems : [ 
                    {display : 'Mã Tài sản', name : 'Ma_tai_san', isdefault : true },
                    {display : 'Tên tài sản', name : 'Ten_tai_san' },
                    {display : 'Mô tả', name : 'Description' },
                    {display : 'Tình trạng', name : 'Type' },
                ],
                sortname : "ItemID",
                sortorder : "asc",
                usepager : true,
                title : 'Bảng tài sản',
                useRp : true,
                rp : 15,
                singleSelect : true,
                showTableToggleBtn : false,
                width : 1080,
                height : 250
            });
        }
        
        function detail_record(command,grid){
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                $.ajax({
                    type: "POST",
                    url: "/QLTSv1/asset/item/detail",
                    data: "ItemID="+id,
                    success: function(data) {
                        //                        alert(data);
                        var getData = $.parseJSON(data);
                        if(getData.status == 'success'){
                            // get user info success
                            var tag = $("<div title='Item\'s information'></div>");
                            tag.html(
                            '<table><tr><td>Ma tai san</td><td>:</td><td>'+getData.data.Ma_tai_san+
                                '</td></tr><tr><td>Ten tai san</td><td>:</td><td>'+getData.data.Ten_tai_san+
                                '</td></tr><tr><td>Description</td><td>:</td><td>'+getData.data.Description+
                                '</td></tr><tr><td>Type</td><td>:</td><td>'+getData.data.Type+
                                '</td></tr><tr><td>Start Date</td><td>:</td><td>'+getData.data.StartDate+
                                '</td></tr><tr><td>Price</td><td>:</td><td>'+getData.data.Price+
                                '</td></tr><tr><td>Warranty Time</td><td>:</td><td>'+getData.data.WarrantyTime+
                                '</td></tr><tr><td>Status</td><td>:</td><td>'+getData.data.Status+
                                '</td></tr><tr><td>Place</td><td>:</td><td>'+getData.data.Place+
                                '</td></tr></table>'
                        ).dialog({
                                resizable: false,
                                height:350,
                                width: 250,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }else{
                            // get user info error
                            var tag = $("<div title='Error'></div>");
                            tag.html("<span>"+getData.msg+"</span>").dialog({
                                resizable: false,
                                height:350,
                                width: 450,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }
                    
                    
                    }
                });
            });
        } // detail_record
<?php if ($uRole == 0) : ?>
            function add_record() {
                $("#dialog-form").attr('title', 'Create new item');
                $("#dialog-form").dialog({
                    autoOpen: false,
                    height: 450,
                    width: 400,
                    modal: true,
                    buttons: {
                        "Create an item": function() {
                            var bValid = true;
                            allFields.removeClass( "ui-state-error" );
                            if ( bValid ) {
                                $.ajax({
                                    type: "POST",
                                    url: "/QLTSv1/asset/item/add",
                                    data: "ItemID="+itemid.val()+"&MaTS="+maTS.val()+"&TenTS="+tenTS.val()+"&Description="+descr.val()+"&Type="+type.val()+"&StartDate="+startDate.val()+"&Price="+price.val()+"&WarrantyTime="+warrantyTime.val()+"&Status="+status.val()+"&Place="+place.val(),
                                    success: function (data){
                                        var getData = $.parseJSON(data);
                                        var tag = $("<div title='"+getData.status+"'></div>");
                                        tag.html("<span>"+getData.msg+"</span>").dialog({
                                            resizable: false,
                                            height:200,
                                            width: 300,
                                            modal: true,
                                            buttons: {
                                                "OK": function() {
                                                    $('.gridTable2').flexReload();
                                                    $('.gridTable3').flexReload();
                                                    $('.gridTable4').flexReload();
                                                    $('.gridTable5').flexReload();
                                                    $( this ).dialog( "close" );
                                                }
                                            }
                                        }).dialog("open");
                                    }
                                });
                                $( this ).dialog( "close" );      
                            }
                                        
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                    close: function() {
                        allFields.val( "" ).removeClass( "ui-state-error" );
                    }
                });
                $( "#dialog-form" ).dialog( "open" );
            }
                
            function delete_record(command,grid)
            {
                var record_count = $('.trSelected',grid).length;
                if (0 == record_count)
                {
                    alert('Please select a record first by clicking on the appropriate row.');
                    return;
                }
                $('.trSelected',grid).each(function(){
                    var id = this.id.substr(3);
                    $( "#dialog-confirm" ).dialog({
                        resizable: false,
                        height:200,
                        width:500,
                        modal: true,
                        buttons: {
                            "Delete" : function() {
                                $.ajax({
                                    type: "POST",
                                    url: '/QLTSv1/asset/item/delete/',
                                    data: 'ItemID='+id,
                                    success: function (data){
                                        var getData = $.parseJSON(data);
                                        var tag = $("<div title='"+getData.status+"'></div>");
                                        tag.html("<span>"+getData.msg+"</span>").dialog({
                                            resizable: false,
                                            height:200,
                                            width: 300,
                                            modal: true,
                                            buttons: {
                                                "OK": function() {
                                                    $('.gridTable2').flexReload();
                                                    $('.gridTable3').flexReload();
                                                    $('.gridTable4').flexReload();
                                                    $('.gridTable5').flexReload();
                                                    $( this ).dialog( "close" );
                                                }
                                            }
                                        }).dialog("open");
                                    }
                                });
                                $( this ).dialog( "close" );
                            },
                            Cancel: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                });
            } // delete_record
<?php endif; ?>
<?php if ($uRole == 0 || $uRole == 1) : ?>
            function edit_record(command,grid)
            {
                var record_count = $('.trSelected',grid).length;
                if (0 == record_count)
                {
                    alert('Please select a record first by clicking on the appropriate row.');
                    return;
                }
                $('.trSelected',grid).each(function(){
                    var id = this.id.substr(3);
                    $.ajax({
                        type: "POST",
                        url: "/QLTSv1/asset/item/detail",
                        data: "ItemID="+id,
                        success: function(data) {
                            //                        alert(data);
                            var getData = $.parseJSON(data);
                            if(getData.status == 'success'){
                                // get user info success
                                $("#ItemID").attr('value', getData.data.ItemID);
                                $("#MaTS").attr('value', getData.data.Ma_tai_san);
                                $("#TenTS").attr('value', getData.data.Ten_tai_san);
                                $("#Description").attr('value', getData.data.Description);
                                $("#Type").attr('value', getData.data.Type);
                                $("#StartDate").attr('value', getData.data.StartDate);
                                $("#Price").attr('value', getData.data.Price);
                                $("#WarrantyTime").attr('value', getData.data.WarrantyTime);
                                $("#Status").attr('value', getData.data.Status);
                                $("#Place").attr('value', getData.data.Place);
                                $("#dialog-form").attr('title', 'Edit user\'s information');
                                $("#dialog-form").dialog({
                                    autoOpen: false,
                                    height: 450,
                                    width: 400,
                                    modal: true,
                                    buttons: {
                                        "Save": function() {
                                            var bValid = true;
                                            allFields.removeClass( "ui-state-error" );
                                            if ( bValid ) {
                                                $.ajax({
                                                    type: "POST",
                                                    url: "/QLTSv1/asset/item/edit",
                                                    data: "ItemID="+itemid.val()+"&MaTS="+maTS.val()+"&TenTS="+tenTS.val()+"&Description="+descr.val()+"&Type="+type.val()+"&StartDate="+startDate.val()+"&Price="+price.val()+"&WarrantyTime="+warrantyTime.val()+"&Status="+status.val()+"&Place="+place.val(),
                                                    success: function (data){
                                                        var getData = $.parseJSON(data);
                                                        var tag = $("<div title='"+getData.status+"'></div>");
                                                        tag.html("<span>"+getData.msg+"</span>").dialog({
                                                            resizable: false,
                                                            height:200,
                                                            width: 300,
                                                            modal: true,
                                                            buttons: {
                                                                "OK": function() {
                                                                    $('.gridTable2').flexReload();
                                                                    $('.gridTable3').flexReload();
                                                                    $('.gridTable4').flexReload();
                                                                    $('.gridTable5').flexReload();
                                                                    $( this ).dialog( "close" );
                                                                }
                                                            }
                                                        }).dialog("open");
                                                    }
                                                });
                                                $( this ).dialog( "close" );      
                                            }
                                        
                                        },
                                        Cancel: function() {
                                            $( this ).dialog( "close" );
                                        }
                                    },
                                    close: function() {
                                        allFields.val( "" ).removeClass( "ui-state-error" );
                                    }
                                });
                                $( "#dialog-form" ).dialog( "open" );
                            } else {
                                // get user info error
                                var tag = $("<div title='Error'></div>");
                                tag.html("<span>"+getData.msg+"</span>").dialog({
                                    resizable: false,
                                    height:300,
                                    width: 400,
                                    modal: true,
                                    buttons: {
                                        "OK": function() {
                                            $( this ).dialog( "close" );
                                        }
                                    }
                                }).dialog("open");
                            }
                        }
                    });
                });
            } // edit_record
<?php endif; ?>
    });
    -->
</script>